package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.mapper.ConversionCoinsRecordMapper;
import com.service.dao.model.ConvertCoinsRecord;
import com.service.scheduling.service.ConvertCoinsRecordService;
import com.service.scheduling.utils.DateUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;


@Service("conversionCoinsRecordService")
public class ConvertCoinsRecordServiceImpl extends ServiceImpl<ConversionCoinsRecordMapper, ConvertCoinsRecord> implements ConvertCoinsRecordService {

    @Override
    public List<ConvertCoinsRecord> listBySomeDay(CoinsEnum coinsEnum, LocalDate someday) {
        Date startTime = DateUtil.getDayOfStart(someday);
        Date endTime = DateUtils.addDays(startTime, 1);

        return list(new QueryWrapper<ConvertCoinsRecord>().lambda()
                .eq(ConvertCoinsRecord::getTargetCoinsId, coinsEnum.getCode())
                .ge(ConvertCoinsRecord::getCreateTime, startTime)
                .lt(ConvertCoinsRecord::getCreateTime, endTime));
    }

    @Override
    public BigDecimal getTotalConversionNum(Integer memberId, CoinsEnum coinsEnum, LocalDate someday) {
        Date startTime = DateUtil.getDayOfStart(someday);
        Date endTime = DateUtils.addDays(startTime, 1);
        return baseMapper.sumConversionNum(memberId, coinsEnum.getName(), startTime, endTime);
    }



}
